package com.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mayifan
 * @version 1.0
 * @date 2025-02-28 14:13
 */
@Configuration
public class MyMQConfig {
/**
 * 容器中的 Binding Queue Exchange 都会自动创建（RabbitMQ没有的情况）

 * RabbitMQ 只要有。@Bean声明属性发生变化也不会覆盖
 */
    /**
     * 订单延时队列
     */


    @Bean
    public Queue orderDelayQueue() {
        // 1、参数
        Map<String, Object> arguments = new HashMap<>();
        // 1.1、设置死信队列的交换机
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        // 1.2、设置死信队列的路由键
        arguments.put("x-dead-letter-routing-key", "order.release.other");
        // 1.3、设置死信延时时间:1分钟
        arguments.put("x-message-ttl", 20000);
        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);
        return queue;
    }

    /**
     * 订单释放队列
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    /**
     * 交换机Topic类型的
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange", true, false);
    }

    /**
     * 绑定延时队列
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseBinding() {

        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }
    /**
     * 绑定释放队列
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }
}
